/*
 * 上海远境文化传媒有限公司 版权所有
 */
package helper;

import facade.AppContext;
import models.base.Corp;
import org.apache.commons.lang3.StringUtils;
import play.Logger;
import play.mvc.Scope;

import java.util.ArrayList;
import java.util.List;

import static util.common.ArrayHelper.safeSplit;

/**
 * Web 资源文件辅助类.
 */
public class WebAssetHelper {

    public static void loadWebAssetRenderArgs(Corp corp) {
        Scope.RenderArgs renderArgs = Scope.RenderArgs.current();

        if (renderArgs == null) {
            // 非Controller，不执行
            Logger.info("LOG04843: 非Controller，不执行 loadWebAssetRenderArgs");
            return;
        }

        // Manager静态页面版本
        renderArgs.put("cssVersion", GlobalConfig.WEB_REACT_CSS_VERSION);
        renderArgs.put("jsVersion", GlobalConfig.WEB_REACT_JS_VERSION);

        // 静态页面版本
        if (StringUtils.isNotBlank(GlobalConfig.WEB_REACT_CSS_URL)) {
            renderArgs.put("cssUrl", GlobalConfig.WEB_REACT_CSS_URL);
        } else {
            String cssAssetUrl = "//a.cdn6.cn/web-react/static/css/main." + GlobalConfig.WEB_REACT_CSS_VERSION + ".css";
            renderArgs.put("cssUrl", cssAssetUrl);
        }

        if (StringUtils.isNotBlank(GlobalConfig.WEB_REACT_JS_URL)) {
            renderArgs.put("jsUrl", GlobalConfig.WEB_REACT_JS_URL);
        } else {
            String jsAssetUrl = "//a.cdn6.cn/web-react/static/js/main." + GlobalConfig.WEB_REACT_JS_VERSION + ".js";
            renderArgs.put("jsUrl", jsAssetUrl);
        }

        String webAssetBase = AppContext.getGlobalConfig(corp, GlobalConfigKeys.WEB_ASSET_BASE);
        if (webAssetBase.startsWith("/")) {
            webAssetBase = UrlHelper.getContextPath(webAssetBase);
        }

        renderArgs.put("webAssetBase", webAssetBase);

        String contextPath = UrlHelper.getDefaultContextPath();
        renderArgs.put("contextPath", contextPath);

        // 自定义资源
        String customWebAssets = corp != null ? corp.customWebAssets : null;
        if (StringUtils.isBlank(customWebAssets)) {
            customWebAssets = AppContext.getGlobalConfig(corp, GlobalConfigKeys.WEB_CUSTOM_ASSETS);
        }
        List<String> customJsUrls = new ArrayList<>();
        List<String> customCssUrls = new ArrayList<>();
        if (StringUtils.isNotBlank(customWebAssets)) {
            String[] splitCustomWebAssetAry = safeSplit(customWebAssets, ",");
            for (String assetPath : splitCustomWebAssetAry) {
                if (assetPath.startsWith("http:") || assetPath.startsWith("https:")) {
                    if (assetPath.endsWith(".js")) {
                        customJsUrls.add(assetPath);
                    }
                    if (assetPath.endsWith(".css")) {
                        customCssUrls.add(assetPath);
                    }
                } else {
                    if (!assetPath.startsWith("/")) {
                        assetPath = "/" + assetPath;
                    }
                    if (assetPath.endsWith(".js")) {
                        customJsUrls.add(webAssetBase + assetPath);
                    }
                    if (assetPath.endsWith(".css")) {
                        customCssUrls.add(webAssetBase + assetPath);
                    }
                }
            }
        }
        renderArgs.put("customJsUrls", customJsUrls);
        renderArgs.put("customCssUrls", customCssUrls);

        renderArgs.put("sentryId", GlobalConfig.SENTRY_ID);
        renderArgs.put("sentryCode", GlobalConfig.SENTRY_CODE);
    }
}
